var DateUtil = {
	estLabourWeek: function(startD,endD) {
        const weeks = [];
        const start = new Date(startD); //起始日期
        const end = new Date(endD); //结束日期
    
        let beginDate = new Date(start.getFullYear(), 0, 1); //起始日期年份的第一天
        let endnDate = new Date(end.getFullYear(), 0, 1); //结束日期年份的第一天

        let bd = beginDate.getDay() >= 4 ? (beginDate.getDay() - 7) : beginDate.getDay()
        let ed = endnDate.getDay() >= 4 ? (endnDate.getDay() - 7) : endnDate.getDay()
        let beginWeek = Math.ceil((parseInt((start - beginDate) / (24 * 60 * 60 * 1000)) + 1 + bd) / 7); //起始日期在当年的周数
        let endWeek = Math.ceil((parseInt((end - endnDate) / (24 * 60 * 60 * 1000)) + 1 + ed) / 7); //结束日期在当年的周数
        let startWeek = beginWeek
        let startYear = start.getFullYear()

        if(start.getFullYear() == end.getFullYear()) {
            if(startWeek == 0) {
                let dateb11 = new Date(startYear-1+'-01-01 00:00:00')
                let date11 = new Date(startYear+'-01-01 00:00:00')
                let date1231 = new Date(startYear-1+'-01-01 00:00:00')
                let beforeWeek = Math.ceil((parseInt((date11 - date1231) / (24 * 60 * 60 * 1000)) + 1 + (dateb11.getDay() >= 4 ? (dateb11.getDay() - 7) : dateb11.getDay())) / 7); //起始年份前一年最后一天在当年的周数
                weeks.push(startYear-1 + "-" + beforeWeek);
                startWeek++;
            } 
             while (startWeek <= endWeek) {
                let str
                let daten11 = new Date(end.getFullYear()+1+'-01-01 00:00:00')
                let nextWeek = Math.ceil((parseInt((new Date(end.getFullYear()+1, 0, 1) - endnDate) / (24 * 60 * 60 * 1000)) + 1 + ed) / 7) //结束年份最后一天在当前年份的周数
                if(startWeek == nextWeek && daten11.getDay() < 4) {
                    str = start.getFullYear()+1 + "-1" 
                } else {
                    str = start.getFullYear() + "-" + startWeek
                }
                weeks.push(str);
                startWeek++;
            }
        } else {
            while (startYear <= end.getFullYear()) {
                let dayMS=24*60*60*1000
                let betDay = new Date(startYear+1+'-01-01 00:00:00').getTime() - new Date(startYear+'-01-01 00:00:00').getTime()
                let newD1 = new Date(startYear,0,1)
                let newD2 = new Date(startYear,11,31)
                let betWeek = betDay / dayMS  - (newD2.getDay()+1) - (7-newD1.getDay())
                let day = 0
                if(newD1.getDay() < 4 && newD1.getDay() > 0) {
                    day += 1
                }
                if(newD2.getDay() >= 4 || newD2.getDay() == 0) {
                    day += 1
                }
                day += (betWeek / 7)
                for(let i = 1; i<=day; i++) {
                    if((startYear == start.getFullYear() && i >= startWeek) || (startYear > start.getFullYear() && startYear < end.getFullYear()) || (startYear == end.getFullYear() && i <= endWeek)) {
                        let str = startYear + "-" + i
                        weeks.push(str);
                    }
                    
                }
                startYear++
            }
        }
        return weeks
    },
    datetoWeek: function(startD) { 
        const weeks = [];
        const start = new Date(startD); //起始日期
    
        let beginDate = new Date(start.getFullYear(), 0, 1); //起始日期年份的第一天

        let bd = beginDate.getDay() >= 4 ? (beginDate.getDay() - 7) : beginDate.getDay()
        let beginWeek = Math.ceil((parseInt((start - beginDate) / (24 * 60 * 60 * 1000)) + 1 + bd) / 7); //起始日期在当年的周数
        let startWeek = beginWeek
        let startYear = start.getFullYear()
        if(startWeek == 0) {
            let dateb11 = new Date(startYear-1+'-01-01 00:00:00')
            let date11 = new Date(startYear+'-01-01 00:00:00')
            let date1231 = new Date(startYear-1+'-01-01 00:00:00')
            let beforeWeek = Math.ceil((parseInt((date11 - date1231) / (24 * 60 * 60 * 1000)) + 1 + (dateb11.getDay() >= 4 ? (dateb11.getDay() - 7) : dateb11.getDay())) / 7); //起始年份前一年最后一天在当年的周数
            startWeek = beforeWeek
            startYear--
        } else {
            
            //year last getDay<4
            //yearlast - date(开始时间) <= getDay
            
            let yearlastday = new Date(startYear + 1, 0, 0) //当年最后一天
            
            let yearlastdayWeek = yearlastday.getDay() //当年最后一天是周几
            let temp = Math.floor((yearlastday - start)/ 86400000)  //Math.floor(nTime / 86400000)
            if(yearlastdayWeek<4 && temp <= yearlastdayWeek){
                let dateb11 = new Date(startYear-1+'-01-01 00:00:00')
                let date11 = new Date(startYear+'-01-01 00:00:00')
                let date1231 = new Date(startYear-1+'-01-01 00:00:00')
                let beforeWeek = Math.ceil((parseInt((date11 - date1231) / (24 * 60 * 60 * 1000)) + 1 + (dateb11.getDay() >= 4 ? (dateb11.getDay() - 7) : dateb11.getDay())) / 7); //起始年份前一年最后一天在当年的周数
                startWeek = beforeWeek
                startYear--
            }
        }
        return `${startYear}-${startWeek}`
    }
}
export { DateUtil }